using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace TaugeLibrary.Classes
{
    public class TaugeDateTime
    {
        private DateTime m_value;
        private DateTime m_value_before;
        public TaugeDateTime(DateTime p_value)
        {
            m_value = p_value;
        }

        public TaugeDateTime()
        {
            IsNull = true;
        }


        public DateTime Value
        {
            set { m_value = value; }
            get { return m_value; }
        }

        public bool IsNull
        {
            get { return (m_value == DateTime.MinValue); }
            set
            {
                if (value)
                {
                    m_value_before = m_value;
                    m_value = DateTime.MinValue;
                }
                else if (m_value == DateTime.MinValue)
                {
                    m_value = m_value_before;
                }
            }
        }

        public string SqlDate
        {
            get
            {
                if (IsNull)
                    return "NULL";
                else
                    return LibConstants.sqlStr(m_value.ToString("MM/dd/yyyy"));
            }
        }

        public string SqlDateTime
        {
            get
            {
                if (IsNull)
                    return "NULL";
                else
                    return LibConstants.sqlStr(m_value.ToString("MM/dd/yyyy HH:mm:ss.fff"));
            }
        }

        public string DateText
        {
            get
            {
                if (IsNull)
                    return "00/00/0000";
                else
                    return m_value.ToString("dd/MM/yyyy");
            }
            set
            {
                CultureInfo nfo = new CultureInfo("en-US");
                try
                {
                    m_value = DateTime.ParseExact(value, "dd/MM/yyyy", nfo);
                }
                catch
                {
                    IsNull = true;
                }

            }
        }

    }
}
